package cn.edu.zhku.util;

import cn.edu.zhku.pojo.Group;
import cn.edu.zhku.pojo.Task;
import cn.edu.zhku.pojo.User;
import cn.edu.zhku.pojo.form.*;
import cn.edu.zhku.service.FormService;
import cn.edu.zhku.service.GroupService;
import cn.edu.zhku.service.TaskService;
import cn.edu.zhku.service.UserService;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class PojoUtil {

    public static PojoUtil pojo = new PojoUtil();

    public static UserUtil user = new UserUtil();

    public static GroupUtil group = new GroupUtil();

    public static TaskUtil task = new TaskUtil();

    public static FormUtil form = new FormUtil();

    private PojoUtil() { }

    public List<Integer> convert2UserIds(Integer userId) {
        List<Integer> userIds = new ArrayList<>();
        userIds.add(userId);
        return userIds;
    }

    public List<Integer> convert2GroupIds(Integer groupId) {
        List<Integer> groupIds = new ArrayList<>();
        groupIds.add(groupId);
        return groupIds;
    }

    public List<Integer> convert2TaskIds(Integer taskId) {
        List<Integer> taskIds = new ArrayList<>();
        taskIds.add(taskId);
        return taskIds;
    }

    public static class UserUtil {

        private UserUtil() { }

        public boolean existUsername(UserService userService, String username) {
            return userService.checkUserExistByUsername(username);
        }

        public boolean existNo(UserService userService, String no) {
            return userService.checkUserExistByNo(no);
        }

        public boolean needUpdateUser(User user) {
            if (StringUtils.hasLength(user.getNo()) || StringUtils.hasLength(user.getRealname()))
                return true;
            return false;
        }

        public boolean needUpdatePassword(UserService userService, User user) {
            User user2 = userService.findPasswordAndSaltById(user.getUserId());
            if (user2 == null)  return false;
            String credential = new Md5Hash(user.getPassword(), user2.getSalt(), 1024).toString();
            if (credential.equals(user2.getPassword()))
                return false;
            return true;
        }
    }

    public static class GroupUtil {

        private GroupUtil() { }

        public boolean notExistGroups(GroupService groupService, List<Integer> groupIds) {
            return !groupService.checkGroupExistByGroupIdBatch(groupIds);
        }

        public boolean notExistGroupUsers(GroupService groupService, Integer groupId, List<Integer> userIds) {
            return !groupService.checkGroupUserExistByGroupIdAndUserIdBatch(groupId, userIds);
        }

        public boolean notExistGroupTasks(GroupService groupService, Integer groupId, List<Integer> taskIds) {
            return !groupService.checkGroupTaskExistByGroupIdAndTaskIdBatch(groupId, taskIds);
        }

        public boolean needUpdateGroup(Group group) {
            if (StringUtils.hasLength(group.getGroupName()) || group.getForbidden()!=null)
                return true;
            return false;
        }

    }

    public static class TaskUtil {

        private TaskUtil() { }

        public boolean notExistTasks(TaskService taskService, List<Integer> taskIds) {
            return !taskService.checkTaskExistByTaskIdBatch(taskIds);
        }

        public boolean notExistTaskGroups(TaskService taskService, Integer taskId, List<Integer> groupIds) {
            return !taskService.checkTaskGroupExistByTaskIdAndGroupIdBatch(taskId, groupIds);
        }

        public boolean needUpdateTask(Task task) {
            if (StringUtils.hasLength(task.getTitle()) || task.getArea() != null
                    || task.getExpireTime()!=null || task.getRemark() != null)
                return true;
            return false;
        }

    }

    public static class FormUtil {

        private FormUtil() { }

        public boolean needUpdateForm(Form form) {
            if (StringUtils.hasLength(form.getPlantNo()) || form.getPlantGrowthStage() != null
            || form.getSubmitTime() != null || form.getState()!=null)
                return true;
            return false;
        }

        public boolean notExistForm(FormService formService, Integer formId) {
            return !formService.checkFormExistById(formId);
        }

    }

}
